package com.lry.basic.metrics;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

public class Id {

    private final UUID id;

    private final String name;

    private final SortedMap<String,String> tags;


    public Id withTag(String name, String value) {
        this.tags.put(name, value);
        return this;
    }

    public Id withTag(Iterable<Map.Entry<String, String>> tags) {
        if (tags != null) {
            for (Map.Entry<String, String> tag : tags) {
                this.tags.put(tag.getKey(), tag.getValue());
            }
        }
        return this;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(name);
        builder.append("(");
        if (tags.size() == 0) {
            builder.append(")");
            return builder.toString();
        }
        for (Map.Entry<String, String> tag : tags.entrySet()) {
            builder.append(String.format("%s=%s,", tag.getKey(), tag.getValue()));
        }
        builder.delete(builder.length() - 1, builder.length());
        builder.append(")");
        return builder.toString();
    }



    public UUID getId(){
        return id;
    }

    public String getName() {
        return name;
    }

    public Iterable<Map.Entry<String, String>> getTags() {
        return tags.entrySet();
    }

    public int getTagCount() {
        return tags.size();
    }

    public Id(String name) {
        this.id = UUID.randomUUID();
        this.name = name;
        this.tags = new TreeMap<>();
    }

}
